//
//  ExternalCommitController.swift
//  GitTFGUI
//
//  Created by Samuel Richard Magny on 2015-09-28.
//  Copyright © 2015 Samuel Richard Magny. All rights reserved.
//

import Cocoa

public class ExternalCommitController: NSObject {

    enum AvailablePreferedClientType: Int, CustomStringConvertible
    {
        case Internal = 0
        case SourceTree
        
        var description : String {
            switch self {
            case .Internal: return "Internal";
            case .SourceTree: return "SourceTree";
                
            }
        }
    }
    
    static let AvailablePreferedClients:Array<AvailablePreferedClientType> = [AvailablePreferedClientType.Internal, AvailablePreferedClientType.SourceTree]
    
    var preferedGitClient:AvailablePreferedClientType {
        get {
            return AvailablePreferedClientType(rawValue: UserSettings.defaultUserSettings.preferedGitClient)!
        }
        set(value) {
            UserSettings.defaultUserSettings.preferedGitClient = value.rawValue
        }
    }
    
    private let userSettings:UserSettings
    
    init(userSettings:UserSettings)
    {
        self.userSettings = userSettings
        super.init()
    }
    
    func commit(repositoryPath:String, completionHandler:(withSuccess:Bool) -> ())
    {
        let clientType = preferedGitClient
        commit(clientType, repositoryPath: repositoryPath, completionHandler: completionHandler)
    }
    
    func isInternalCommitEnabled() -> Bool {
        
        return (userSettings.preferedGitClient == AvailablePreferedClientType.Internal.rawValue)
    }
    
    // Privates
    //---------------------------------------------------------------------------
    private func commit(clientType:AvailablePreferedClientType, repositoryPath:String, completionHandler:(withSuccess:Bool) -> ())
    {
        if let commitCompliant = commitClassForClientType(clientType)
        {
            commitCompliant.commit(repositoryPath, completionHandler: completionHandler)
        }
    }
    
    private func commitClassForClientType(clientType:AvailablePreferedClientType)->ExternalCommitLoaderCompliant?
    {
        switch(clientType)
        {
        case .SourceTree:
            return SourceTreeCommitLoader() as ExternalCommitLoaderCompliant
            
        default:
            return nil
        }

    }
    
}
